package com.neu.cloudFactory.controller;

import com.neu.cloudFactory.PO.Bid;
import com.neu.cloudFactory.PO.Order;
import com.neu.cloudFactory.PO.User;
import com.neu.cloudFactory.VO.BidInfoVO;
import com.neu.cloudFactory.VO.OrderVO;
import com.neu.cloudFactory.service.FactoryService;
import com.neu.cloudFactory.service.OrderService;
import com.neu.cloudFactory.service.UserService;
import com.neu.cloudFactory.utils.result.Result;
import com.neu.cloudFactory.utils.result.ResultFactory;
import net.sf.ehcache.CacheOperationOutcomes;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order")
public class OrderController {
   final OrderService orderService;
   final FactoryService factoryService;
   final UserService userService;

    public OrderController(OrderService orderService, FactoryService factoryService, UserService userService) {
        this.orderService = orderService;
        this.factoryService = factoryService;
        this.userService = userService;
    }

    //三种订单列表

    @CrossOrigin
    @GetMapping(value = "/listAdminOrder")
    @ResponseBody
    public Result listAdminOrder(){
        List<OrderVO> orderVOList = orderService.findAllOrder();
        return ResultFactory.buildSuccessResult(orderVOList);
    }


    @CrossOrigin
    @PostMapping(value = "/listCustomerOrder")
    @ResponseBody
    public Result listCustomerOrder(@RequestBody User user){
        System.out.println(user);
        List<OrderVO> orderVOList = orderService.findAllOrderForCustomer(user.getAccount());
        System.out.println(orderVOList);
        return ResultFactory.buildSuccessResult(orderVOList);
    }

    @CrossOrigin
    @GetMapping(value = "/listFactoryOrder")
    @ResponseBody
    public Result listFactoryOrder(){
        List<OrderVO> orderVOList = orderService.findAllOrderForFactory();
        return ResultFactory.buildSuccessResult(orderVOList);
    }

    //新增订单
    @CrossOrigin
    @PostMapping(value = "/addOrder")
    @ResponseBody
    public Result addOrder(@RequestBody Order order){
        System.out.println(order);
        //自动生成编号
        order.setCode(orderService.generateCode());
        order.setFactoryId(17);//暂存为产能中心的id
        orderService.addOrder(order);
        return ResultFactory.buildSuccessResult(orderService.addIdToOrder(order),"添加成功");
    }

    //删除订单
    @CrossOrigin
    @PostMapping(value = "/deleteOrder")
    @ResponseBody
    public Result deleteOrder(@RequestBody OrderVO orderVO){
            Order order = orderService.changeOrderType(orderVO);
            order.setId(Integer.parseInt(orderVO.getId()));
            orderService.deleteOrder(order);
            return ResultFactory.buildSuccessResult("删除成功");
        }

    //云工厂投标
    @CrossOrigin
    @PostMapping(value = "/putBid")
    @ResponseBody
    public Result putBid(@RequestBody Bid bid){
        orderService.addBid(bid);
        return ResultFactory.buildSuccessResult("投标成功");
    }

//    经销商选标
    @CrossOrigin
    @PostMapping(value = "/chooseBid")
    @ResponseBody
    public Result chooseBid(@RequestBody BidInfoVO bidInfoVO){
        Bid bid = orderService.changeBidType(bidInfoVO);
        orderService.chooseBid(bid);
        return ResultFactory.buildSuccessResult("选标成功");
    }

    //投标详情
    @CrossOrigin
    @PostMapping(value = "/allBid")
    @ResponseBody
    public Result allBid(@RequestBody OrderVO orderVO){
        Order order = orderService.changeOrderType(orderVO);
        order.setId(Integer.parseInt(orderVO.getId()));
        return ResultFactory.buildSuccessResult(orderService.findAllBid(order));
    }
    //修改订单状态
    @CrossOrigin
    @PostMapping(value = "/modifyState")
    @ResponseBody
    public Result modifyState(@RequestBody Order order){
        System.out.println(order);
        orderService.modifyFactoryState(order,order.getOrderStateForFactory());
        orderService.modifyCustomerState(order,order.getOrderStateForCustomer());
        System.out.println(order);
        return ResultFactory.buildSuccessResult("修改成功");
    }

}
